package com.echat.business.book.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.echat.business.book.model.Book;
import com.echat.business.book.model.Discussion;
import com.echat.business.book.service.IBookService;
import com.echat.business.book.service.IDiscussionService;
import com.echat.business.home.model.SysUser;
import com.echat.config.common.RequestHolder;
import com.echat.core.constant.ConstantUtils;
import com.echat.core.constant.response.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project BookController
 * @Title BookController.java
 * @Description 书籍阅读控制器
 * @Package com.echat.business.book.controller
 * @date 2019/6/6 13:26
 * @Copyright: 上海顺益信息科技有限公司 All rights reserved.
 */
@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    @Autowired
    private IDiscussionService discussionService;

    @RequestMapping(value = "/hotBookList")
    public ResponseData getHotBookList() {
        Wrapper<Book> wrapper = new EntityWrapper<>();
        List<Book> bookList = bookService.selectList(wrapper);
        return new ResponseData(true, 200, "获取书籍列表成功", bookList);
    }

    @RequestMapping(value = "/getDiscussion")
    public ResponseData getDiscussion(@RequestParam("bookId") Integer bookId, @RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        Page<Discussion> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        List<Discussion> discussions = discussionService.getDiscussionAndReplyByBookId(page, bookId);
        page.setRecords(discussions);
        return new ResponseData(true, 200, "获取评论信息列表成功", page);
    }

    @RequestMapping(value = "/getBookProps")
    public ResponseData getBookProps(Integer bookId) {
        Book book = bookService.selectById(bookId);
        return new ResponseData(true, 200, "获取书籍对象成功", book);
    }

    @RequestMapping(value = "/getNewBookList")
    public ResponseData getNewBookList() {
        Wrapper<Book> wrapper = new EntityWrapper<>();
        wrapper.orderBy("id desc limit 8");
        List<Book> books = bookService.selectList(wrapper);
        return new ResponseData(true, 200, "获取最新上架书籍列表成功", books);
    }

    @RequestMapping(value = "/doComment")
    public ResponseData doComment(Discussion discussion) {
        SysUser loginUser = RequestHolder.getCurrentUser();
        String content = discussion.getContent();
        Integer bookId = discussion.getBookId();
        Integer userId = loginUser.getId();
        //插入用户评论
        Discussion discussionEntity = new Discussion();
        discussionEntity.setBookId(bookId);
        discussionEntity.setContent(content);
        discussionEntity.setUserId(userId);
        discussionEntity.setCreateTime(new Date());
        discussion.setDelflag(ConstantUtils.Public.ZERO);
        discussionService.insert(discussionEntity);
        return new ResponseData(true, 200, "评论成功", null);
    }

    @RequestMapping(value = "/hotCommentsList")
    public ResponseData getHotCommentsList() {
        List<Map<String, Object>> hotCommentBooksList = bookService.getHotCommentsList();
        return new ResponseData(true, 200, "获取热评书籍列表成功", hotCommentBooksList);
    }
}
